﻿using BAL;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Module.QLKhachHang
{
    public partial class frmQLKhachHang : Form
    {
        public KhachHangBAO khBAO = new KhachHangBAO();
        public frmQLKhachHang()
        {
            InitializeComponent();
        }
        public void loadData()
        {
            List<KhachHang> listKH = khBAO.KhachHang_GetData();
            DGV.DataSource = listKH;
            for (int i = 0; i < DGV.Rows.Count; i++)
            {
                DGV.Rows[i].Cells[0].Value = (i + 1);

            }
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            loadData();
            DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            bt_update.Enabled = false;
            btDelete.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaKH.Text.Length == 0) throw new Exception("Mã Khách Hàng không được bỏ trống"); txtMaKH.Focus();
                if (txtHoDem.Text.Length == 0) throw new Exception("Họ Đệm khách hàng không được bỏ trống"); txtHoDem.Focus();
                if (txtTen.Text.Length == 0) throw new Exception("Tên khách hàng không được bỏ trống"); txtTen.Focus();
                if (txtDiaChi.Text.Length == 0) throw new Exception("Địa chỉ khách hàng không được bỏ trống"); txtDiaChi.Focus();
                if (txtMST.Text.Length == 0) throw new Exception("Mã số thuế khách hàng không được bỏ trống"); txtMST.Focus();
                if (txtCMND.Text.Length == 0) throw new Exception("CMND khách hàng không được bỏ trống"); txtCMND.Focus();
                KhachHang kh = new KhachHang();
                getInfo(kh);
                khBAO.KhachHang_AddNew(kh);
                loadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR");
            }
        }
        public void getInfo(KhachHang kh)
        {

            kh.maKhachHang = txtMaKH.Text;
            kh.hoDemKH = txtHoDem.Text;
            kh.tenKH = txtTen.Text;
            kh.SDT = txtSDT.Text;
            kh.diaChi = txtDiaChi.Text;
            kh.gioiTinh = CboGT.Text;
            kh.maSoThue = txtMST.Text;
            kh.ngaySinh = dtNgaySinh.Value;
            kh.CMND = txtCMND.Text;
            kh.ghiChu = txtGhiChu.Text;
            kh.loaiKH = cboLoaiKH.Text; ;

        }

        private void bt_update_Click(object sender, EventArgs e)
        {

            KhachHang kh = new KhachHang();
            getInfo(kh);
            khBAO.KhachHang_Update(kh);
            loadData();
            Reset();
        }

        private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaKH.Text = Convert.ToString(DGV.CurrentRow.Cells[1].Value);
            txtCMND.Text = Convert.ToString(DGV.CurrentRow.Cells[2].Value);
            txtHoDem.Text = Convert.ToString(DGV.CurrentRow.Cells[3].Value);
            txtTen.Text = Convert.ToString(DGV.CurrentRow.Cells[4].Value);
            txtDiaChi.Text = Convert.ToString(DGV.CurrentRow.Cells[5].Value);
            txtSDT.Text = Convert.ToString(DGV.CurrentRow.Cells[6].Value);
            dtNgaySinh.Value = Convert.ToDateTime(DGV.CurrentRow.Cells[7].Value);
            CboGT.Text = Convert.ToString(DGV.CurrentRow.Cells[8].Value);
            txtMST.Text = Convert.ToString(DGV.CurrentRow.Cells[9].Value);
            cboLoaiKH.Text = Convert.ToString(DGV.CurrentRow.Cells[10].Value);
            txtGhiChu.Text = Convert.ToString(DGV.CurrentRow.Cells[11].Value);
            txtMaKH.Enabled = false;
            txtHoDem.Enabled = false;
            txtTen.Enabled = false;
            dtNgaySinh.Enabled = false;
            CboGT.Enabled = false;
            txtMST.Enabled = false;
            bt_addNew.Enabled = false;
            txtCMND.Enabled = false;
            bt_update.Enabled = true;
            btDelete.Enabled = true;
            txtDiaChi.Enabled = true;
            txtSDT.Enabled = true;
            cboLoaiKH.Enabled = true;
            txtGhiChu.Enabled = true;
            txtsearch.Enabled = false;
            btsearch.Enabled = false;

        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            KhachHang kh = new KhachHang();
            getInfo(kh);
            khBAO.KhachHang_Delete(kh);
            loadData();
            Reset();
        }

        private void btsearch_Click(object sender, EventArgs e)
        {

            List<KhachHang> listKH1 = khBAO.KhachHang_SearchCustomer(txtsearch.Text);
            if (listKH1.Count == 0)
            {
                MessageBox.Show("Không tồn tại khách hàng ", "ERROR");
                Reset();
            }
            else
            {
                DGV.DataSource = listKH1;
                for (int i = 0; i < DGV.Rows.Count; i++)
                {
                    DGV.Rows[i].Cells[0].Value = (i + 1);

                }
            }
        }

        private void txtsearch_Click(object sender, EventArgs e)
        {
            txtMaKH.Enabled = false;
            txtHoDem.Enabled = false;
            txtTen.Enabled = false;
            dtNgaySinh.Enabled = false;
            CboGT.Enabled = false;
            txtMST.Enabled = false;
            bt_addNew.Enabled = false;
            txtCMND.Enabled = false;
            txtDiaChi.Enabled = false;
            txtGhiChu.Enabled = false;
            txtSDT.Enabled = false;
            cboLoaiKH.Enabled = false;
            bt_update.Enabled = false;
            btDelete.Enabled = false;
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        public void Reset()
        {
            txtMaKH.Enabled = true;
            txtHoDem.Enabled = true;
            txtTen.Enabled = true;
            dtNgaySinh.Enabled = true;
            CboGT.Enabled = true;
            txtMST.Enabled = true;
            bt_addNew.Enabled = true;
            txtCMND.Enabled = true;
            txtDiaChi.Enabled = true;
            txtGhiChu.Enabled = true;
            txtSDT.Enabled = true;
            cboLoaiKH.Enabled = true;
            bt_update.Enabled = false;
            btDelete.Enabled = false;
            txtMaKH.Clear();
            txtHoDem.Clear();
            txtTen.Clear();
            txtCMND.Clear();
            txtDiaChi.Clear();
            txtGhiChu.Clear();
            txtMST.Clear();
            txtSDT.Clear();
            CboGT.Text = "";
            cboLoaiKH.Text = "";
            txtsearch.Clear();
            dtNgaySinh.Value = DateTime.Now;
            txtsearch.Enabled = true;
            btsearch.Enabled = true;
            loadData();
        }

        private void txtsearch_TextChanged(object sender, EventArgs e)
        {

        }

       

    }
}
